package com.punai.web.controller.ep;

import com.punai.common.annotation.Anonymous;
import com.punai.common.constant.Constants;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.entity.SysDept;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.SecurityUtils;
import com.punai.framework.web.service.SysLoginService;
import com.punai.system.service.EpWxService;
import com.punai.system.util.WeChatComUtil;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeChatEnterpriseQrcodeRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ep/wx")
public class WxController extends BaseController {

    @Autowired
    private EpWxService epWxService;

    @Autowired
    private WeChatComUtil weChatComUtil;

    @Autowired
    private SysLoginService loginService;

    @PreAuthorize("@ss.hasPermi('system:user:refresh')")
    @RequestMapping("syncUser")
    @ResponseBody
    public AjaxResult syncUser() throws ServiceException {
        boolean process = epWxService.isProcess();
        if (process) {
            return AjaxResult.error("正在同步部门及用户中");
        }
        return AjaxResult.success(epWxService.syncUserByDeptId());
    }

    @RequestMapping("syncDepartment")
    @ResponseBody
    public AjaxResult syncDepartment() throws ServiceException {
        boolean process = epWxService.isProcess();
        if (process) {
            return AjaxResult.error("正在同步部门及用户中");
        }
        return toAjax(epWxService.syncDepartment(SecurityUtils.getCompanyId()));
    }

    @RequestMapping("getWxLoginParams")
    @ResponseBody
    @Anonymous
    public AjaxResult getWxLoginParams() throws ServiceException {
        Map<String,String> resMap = new HashMap<>();
        resMap.put("clientId",weChatComUtil.getCorpid());
        resMap.put("clientSecret",weChatComUtil.getPlatformSecret());
        // resMap.put("redirectUri","http://localhost/login");
        resMap.put("agentId",weChatComUtil.getPlatformId());
        return AjaxResult.success(resMap);
    }



    @RequestMapping("/render")
    @Anonymous
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        String authorize = authRequest.authorize(AuthStateUtils.createState());
        System.out.println(authorize);
        // response.sendRedirect();
    }



    @RequestMapping("/login/{code}")
    @Anonymous
    public AjaxResult login(@PathVariable String code) {
        AjaxResult ajax = AjaxResult.success();
        String userId = weChatComUtil.getLoginUser(code);
        String token = loginService.login(userId);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    private AuthRequest getAuthRequest() {
        return new AuthWeChatEnterpriseQrcodeRequest(AuthConfig.builder()
                .clientId(weChatComUtil.getCorpid())
                .clientSecret(weChatComUtil.getPlatformSecret())
                .redirectUri("http://127.0.0.1:8080/ep/wx/callback")
                .agentId(weChatComUtil.getPlatformId())
                .build());
    }

}
